import * as goodsService from '../services/goods';

import { Toast } from 'antd-mobile'
import { Link, browserHistory } from 'dva/router';


export default {

  namespace: 'goods',

  state: {
    brandDetail: null
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
    }
  },

  effects: {
    *addCart({ payload }, { call, put }) {
      const { data } = yield call(goodsService.addCart, payload) 
      
      if(data && data.status === 1) {
        Toast.info('添加购物车成功', 1)
        // window.setTimeout(() => {
          browserHistory.push('/');
          yield put({ type: 'meyley/changeHomeTabType', payload: 'goods' });
        // }, 1000)
      } else {
        Toast.info(`${data && data.msg || '添加购物车失败，请稍后再试'}`);
      }
    },
    *getBrandDetail({ payload }, { call, put }) {
      const { data } = yield call(goodsService.getBrandDetail, payload)
        
      yield put({
        type: 'saveBrandDetail',
        payload: {
          brandDetail: data && data.result || null
        }
      })
    },
  },

  reducers: {
    saveOrderList(state, { payload: { orderList }}) {
      return { ...state, orderList }
    },
    saveOrderDetail(state, { payload: { orderDetail }}) {
      return { ...state, orderDetail }
    },
    emptyOrderList(state) {
      return { ...state, orderList: null } 
    },
    saveBrandDetail(state, { payload: { brandDetail }}) {
      return { ...state, brandDetail }
    }
  }

};
